
struct MaterialSource
{
	vec4 Diffuse;
	vec3 Ambient;
	float Shininess;
	vec3 Specular;
	float EnvCoeff;
	vec3 Emissive;
	float tmp;
};

uniform ViewParams 
{
	mat4 uViewMatrix;
	mat4 uProjectionMatrix;
	vec3 uEyePosition;
	vec3 uViewDirection;
};

uniform samplerBuffer uMatrixBuffer;

mat4 GetModelMatrix(int index)
{
	return transpose(mat4(texelFetch(uMatrixBuffer, 4*index + 0),
						  texelFetch(uMatrixBuffer, 4*index + 1),
						  texelFetch(uMatrixBuffer, 4*index + 2),
						  texelFetch(uMatrixBuffer, 4*index + 3)));
}

mat3 FormNormalMatrix(mat4 src, bool ortho)
{
	if (ortho)
		return mat3(src);
	else
		return mat3(transpose(inverse(src)));
}

mat3 GetNormalMatrix(int index)
{
	return FormNormalMatrix(GetModelMatrix(index), false);
}

mat3 GetMatrixTBN(mat3 o2eMatrix, vec3 oNormal, vec3 oTangent, bool bDoubleSided)
{
	mat3 res;
		
	// Set up tangent-space matrix -- btn
	res[2] = normalize(o2eMatrix * oNormal); // normal
	res[0] = normalize(o2eMatrix * oTangent); // tangeant
	res[1] = normalize(cross(res[0], res[2])); // binormal
	res[0] = normalize(cross(res[2], res[1])); // binormal
		
	if ((res[2].z < 0.0) && bDoubleSided)
		res[2] = -res[2];

	return res;
}
